package com.foreverwin.jzymes.common.service.impl;

import com.foreverwin.jzymes.common.constant.Constants;
import com.foreverwin.jzymes.common.domain.SfcBatchPassLog;
import com.foreverwin.jzymes.common.enums.SfcBatchPassLogEnum;
import com.foreverwin.jzymes.common.mapper.SfcBatchPassLogAdviceMapper;
import com.foreverwin.jzymes.common.mapper.SfcBatchPassLogMapper;
import com.foreverwin.jzymes.common.service.*;
import com.foreverwin.jzymes.common.util.BOHandleUtil;
import com.foreverwin.jzymes.framework.util.I18nUtil;
import com.foreverwin.jzymes.production.sfc.domain.SFC;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.List;

@Service
public class SfcBatchPassLogAdviceServiceImpl implements ISfcBatchPassLogAdviceService {

    @Autowired
    ICommonService commonService;
    @Autowired
    IResourceAdviceService resourceAdviceService;
    @Autowired
    IItemGroupAdviceService itemGroupAdviceService;
    @Autowired
    ISystemRuleAdviceService systemRuleAdviceService;
    @Autowired
    SfcBatchPassLogAdviceMapper sfcBatchPassLogAdviceMapper;
    @Autowired
    SfcBatchPassLogMapper sfcBatchPassLogMapper;

    private int sfcBatchPassQty = 0;
    private int sfcBatchPassInterval = 0;

    public void initSystemRule( String site ){
        if( sfcBatchPassQty == 0 ){
            sfcBatchPassQty = Integer.parseInt(systemRuleAdviceService.findSystemRule( site, Constants.SYSTEM_RULE_SFC_BATCH_PASS_QTY, "50" ));
        }
        if( sfcBatchPassInterval == 0 ){
            sfcBatchPassInterval = Integer.parseInt(systemRuleAdviceService.findSystemRule( site, Constants.SYSTEM_RULE_SFC_BATCH_PASS_INTERVAL, "10" ));
        }
    }

    /**
     * 查询工令打开状态批次过站记录
     *
     * @param site
     * @param sfc
     * @param resource
     * @return
     */
    public SfcBatchPassLog findOpenSfcBatchPassLog( String site, String sfc, String resource, String operation ){
        Assert.hasText( site, I18nUtil.getI18nText( "common.siteNotBlank" ));
        Assert.hasText( sfc, I18nUtil.getI18nText( "common.sfcNotBlank" ));
        Assert.hasText( resource, I18nUtil.getI18nText( "common.resourceNotBlank" ));

        SfcBatchPassLog sfcBatchPassLog = sfcBatchPassLogAdviceMapper.selectOpenSfcBatchPassLog( site, sfc, resource, operation );
        return sfcBatchPassLog;
    }

    /**
     * 查询打开状态批次过站记录
     *
     * @param site
     * @param itemBo
     * @param resource
     * @return
     */
    public SfcBatchPassLog findOpenSfcBatchPassLogListByOperation( String site, String itemBo, String resource ){
        Assert.hasText( site, I18nUtil.getI18nText( "common.siteNotBlank" ));
        Assert.hasText( itemBo, I18nUtil.getI18nText( "common.itemNotBlank" ));
        Assert.hasText( resource, I18nUtil.getI18nText( "common.resourceNotBlank" ));

        String operation = resourceAdviceService.findOperationByResource( site, resource );
        return sfcBatchPassLogAdviceMapper.selectOpenSfcBatchPassLogByOperation( site, itemBo, resource, operation );
    }

    /**
     * 保存工令批次过站记录
     *
     * @param sfc
     * @param resource
     * @param passQty
     * @param passDate
     */
    public void saveSfcBatchPass( String site, String sfc, String itemBo, String resource, Long passQty, Date passDate ){
        String operation = resourceAdviceService.findOperationByResource( site, resource );
        saveSfcBatchPass( site, sfc, itemBo, resource, operation, passQty, passDate );
    }

    /**
     * 保存工令批次过站记录
     *
     * @param sfc
     * @param resource
     * @param passQty
     * @param passDate
     */
    public void saveSfcBatchPass( String site, String sfc, String itemBo, String resource, String operation, Long passQty, Date passDate ){
        SfcBatchPassLog sfcBatchPassLog = null;
        initSystemRule( site );
        if( StringUtils.isNotBlank( sfc ) ){
            /**查询工令打开状态批次过站记录*/
            sfcBatchPassLog = findOpenSfcBatchPassLog( site, sfc, resource, operation );
        }

        if( sfcBatchPassLog != null ){
            /**更新打开状态批次过站记录数量，超过指定数量状态改为待过站状态*/
            updateSfcBatchPassLog( sfcBatchPassLog, passQty, passDate );
        }else {
            /**新增打开状态批次过站记录*/
            insertSfcBatchPassLog( site, sfc, itemBo, operation, resource, passQty, passDate );
        }
    }

    public void updateSfcBatchPassLog( SfcBatchPassLog sfcBatchPassLog, Long qty, Date date ){
        sfcBatchPassLog.setQty( sfcBatchPassLog.getQty() + qty );
        sfcBatchPassLog.setUpdatedDateTime( date );
        if( sfcBatchPassLog.getQty() >= sfcBatchPassQty ){
            sfcBatchPassLog.setStatus(SfcBatchPassLogEnum.PENDING.getValue() );
        }else if(  sfcBatchPassLog.getQty() > 0 && DateUtils.addMinutes( sfcBatchPassLog.getCreatedDateTime(), sfcBatchPassInterval ).compareTo( new Date() ) <= 0 ){
            sfcBatchPassLog.setStatus(SfcBatchPassLogEnum.PENDING.getValue() );
        }
        sfcBatchPassLogMapper.updateSfcBatchPassLog( sfcBatchPassLog );
    }

    public void insertSfcBatchPassLog( String site, String sfc, String itemBo, String operation, String resource, Long qty, Date date ){
        SfcBatchPassLog sfcBatchPassLog = new SfcBatchPassLog();
        sfcBatchPassLog.setSite( site );
        sfcBatchPassLog.setMachineType( itemGroupAdviceService.findItemGroupBySite( site ) );
        sfcBatchPassLog.setSfc( sfc );
        sfcBatchPassLog.setItemBo( itemBo );
        sfcBatchPassLog.setOperation( operation );
        sfcBatchPassLog.setResrce( resource );
        sfcBatchPassLog.setQty( qty );
        sfcBatchPassLog.setStatus( SfcBatchPassLogEnum.OPEN.getValue() );
        sfcBatchPassLog.setCreatedDateTime( date );
        sfcBatchPassLog.setHandle( BOHandleUtil.buildSfcBatchPassLogHandle() );
        sfcBatchPassLogMapper.insertSfcBatchPassLog( sfcBatchPassLog );
    }
}
